package controller

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/e421083458/gorm"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/container/gtype"
	"github.com/gogf/gf/util/gconv"
	"rock/customer/dto"
	"rock/customer/initParam"
	"rock/customer/middleware"
	"rock/customer/models"
	"rock/customer/modules/redisModules"
	"rock/customer/modules/taskMap"
	"rock/customer/public"
	"strconv"
	"time"
)

// @Summary 新增机器人
// @Description
// @Tags 机器人
// @ID /createrobot
// @Accept  json
// @Produce  json
// @Param polygon body dto.Robot true "body"
// @Success 200 {object} middleware.Response{} "success"
// @Router /createrobot [post]
func (g *RobotController) createRobot(router *gin.Context) {
	params := &dto.Robot{}
	if err := params.BindingParams(router); err != nil {
		middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
		return
	}
	//创建机器人
	robotMsg := &models.WkbImRobot{
		RobotCode:         params.RobotCode,
		RobotNickname:     params.RobotNickname,
		RobotImg:          params.RobotImg,
		WelcomeFlag:       params.WelcomeFlag,
		WelcomeText:       params.WelcomeText,
		AnswerFlag:        params.AnswerFlag,
		AnswerTitle:       params.AnswerTitle,
		AnswerText:        params.AnswerText,
		KeywordAutoAnswer: params.KeywordAutoAnswer,
		KeywordAutoLink:   params.KeywordAutoLink,
	}
	err := robotService.CreateRobot(router, BusinessDB, robotMsg)
	if err != nil {
		middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
		return
	}
	middleware.ResponseSuccess(router, gin.H{"msg": initParam.DealSuccessText})
}

// @Summary 机器人发送欢迎语（供后端调用）,以及测试发送私聊（需要传UserId和OtherUserId）
// @Description 传参只需要传GroupId，MsgType 消息类型 1 发送客户群消息（机器人发送欢迎语消息） 2 客服给客服发消息(小灰条消息) 3 发送转接消息 4 模拟客户给群发送消息 5 模拟客服给群发送消息 6 系统消息 7 客服发消息给客服
// @Tags 机器人
// @ID /sendwelcomemsg
// @Accept  json
// @Produce  json
// @Param polygon body dto.Group true "body"
// @Success 200 {object} middleware.Response{} "success"
// @Router /sendwelcomemsg [post]
func (g *RobotController) sendWelcomeMsg(router *gin.Context) {
	params := &dto.Group{}
	if err := params.BindingParams(router); err != nil {
		middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
		return
	}
	//消息类型
	// Server API 发送的消息, 默认发送方是不会触发消息监听的;或者在 Server API 发消息时, isIncludeSender 填 1, 则发送者也可收到发送的消息
	// 1 【机器人】机器人发送欢迎【语】、机器人发送应答链接欢迎【语】
	// 4 【机器人】邀请之后入群的 【提示】 -- XX为您服务、邀请之后入群的 【提示】 -- 请问有什么可以帮到你？
	// 5 【客服】客服给客服发消息 【提示】
	// 7 【客服】发送邀请询问消息 【框】
	// 8 【机器人】系统消息 【提示】
	// 9 【客服】发送转接询问 【框】
	// 10 【机器人】发送【正在邀请】状态 【提示】 告知用户
	// 11 【客户】确认是否转接给予到客服的 【提示】
	// 12 【机器人】发送关键词触发 【语】
	// 13 【客服】发送一条合并消息 【框】
	// 13 【客服】发送一条合并消息 【框】
	// 14 【机器人】发送【正在转接】状态 【提示】 告知用户
	// 15 【客户】确认是否受邀给予到客服的 【提示】
	// 16 【机器人】时间截断 【提示】
	// 17 【机器人】客服在没有聊天的情况下，系统与客服发送的一个欢迎内容
	// 18 【客户】超时未接受邀请，超时未接受转接
	rcParam := ""
	//检查是否需要发送时间块
	key := params.GroupId + "_sendTimeBlock"
	if taskMap.M.Get(key) != nil {
		value := taskMap.M.Get(key).(taskMap.SendMap)
		if value.TimeStamp.Val() < gconv.Int(time.Now().Unix()) {
			err := sendTimeBlock(router, params.GroupId)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
			timeout := gconv.String(time.Now().Add(taskMap.SendTimeBlockTime).Unix())
			taskMap.M.Set(key, taskMap.SendMap{
				ToUserID:  gtype.NewString(params.GroupId),
				TimeStamp: gtype.NewInt(gconv.Int(timeout)),
				Type:      gtype.NewString("sendTimeBlock"),
			})
		}
	}
	quitGroupUserId := make([]string, 0)
	noticeUserId := make([]string, 0)
	invisibleUserId := make([]string, 0)
	//
	groupRobot, err := groupService.GroupRobot(router, BusinessDB, params.GroupId)
	if err != nil {
		if err.Error() == initParam.DataIsNotExist {
			middleware.ResponseError(router, middleware.DATA_NOT_EXIST, err)
			return
		}
		middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
		return
	}
	//
	//

	switch params.MsgType {
	case 1:
		// 1 【机器人】机器人发送欢迎【语】、机器人发送应答链接欢迎【语】
		links := make([]map[string]string, 0)
		if err != nil {
			if !errors.Is(err, gorm.ErrRecordNotFound) {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		// 机器人发送欢迎
		if groupRobot.WelcomeFlag == 1 {
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := groupRobot.WelcomeText
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		// 发送应答链接欢迎语
		if groupRobot.AnswerFlag == 1 {
			keyWordJump, err := serviceService.KeyWordListJump(router, InstanceDB)
			if err != nil {
				if !errors.Is(err, gorm.ErrRecordNotFound) {
					middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
					return
				}
			}
			for _, vv := range keyWordJump {
				links = append(links, map[string]string{
					"id":      vv.GroupId,
					"keyword": vv.Keyword,
				})
			}
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := groupRobot.AnswerTitle
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, contentStr)
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 4:
		// 4 【机器人】邀请之后入群的 【提示】 -- XX为您服务、邀请之后入群的 【提示】 -- 请问有什么可以帮到你？
		groupMemberAdmin, err := groupService.GroupMemberAdmin(router, BusinessDB, params.GroupId)
		if err != nil {
			dealRequestError(router, err)
			return
		}
		admin := initParam.CsTarget + gconv.String(groupMemberAdmin.UserID)

		links := make([]map[string]string, 0)
		// 发送入群提示语 请问有什么可以帮到你？
		{
			fromUserId := initParam.RobotTarget + gconv.String(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			//msgContent := robotQuestionMsg
			//msgContent := "您好，我是创易栈的" + params.Msg + "，很高兴为您服务，请问有什么可以帮到你？"
			msgContent := params.Msg + " " + initParam.ServiceText
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:    msgContent,
				Links:      links,
				ObjectName: objectName,
				ChatType:   "group",
				Members:    members,
				Style:      "refreshList",
				NoticeUserId: []string{
					admin,
				},
				QuitGroupUserId: quitGroupUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		redisModules.RedisSendIntroductionText("set", "SendIntroductionText_"+params.GroupId, 1)
		break
	case 7:
		// 7 【客服】发送邀请询问消息 【框】
		//给客服发送一个转接消息
		//有聊天则直接发送到群
		//没有聊天则先创建聊天，再发送消息
		groupId, err := makePrivateGroupBeforeTalk(router, params.UserId, params.OtherUserId)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		{
			fromUserId := initParam.CsTarget + params.UserId
			toGroupId := groupId
			objectName := initParam.ChatMsgTxt
			links := make([]map[string]string, 0)
			csUser, err := customerServiceService.CsUser(router, InstanceDB, gconv.Int(params.UserId))
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
			members := dto.MsgMembers{
				UserName: csUser.RealName,
				//UserAvatar: user.Avatar,
				UserAvatar: initParam.FAEAvatar,
			}
			groupMemberC, err := groupService.GroupMemberC(router, BusinessDB, params.GroupId)
			if err != nil {
				dealRequestError(router, err)
				return
			}
			//msgContent := user.RealName + initParam.InvitationMessageText
			//msgContent := initParam.InvitationMessageText
			msgContent := initParam.Customer + groupMemberC.MemberName + initParam.InvitationMessageText
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				Members:         members,
				Style:           "invitation",
				Extra:           params.GroupId,
				ObjectName:      objectName,
				ChatType:        "group",
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 8:
		// 8 【机器人】系统消息 【提示】
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := params.Msg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 9:
		// 9 【客服】发送转接询问 【框】
		//给客服发送一个转接消息
		//有聊天则直接发送到群
		//没有聊天则先创建聊天，再发送消息
		groupId, err := makePrivateGroupBeforeTalk(router, params.UserId, params.OtherUserId)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		//发送转接消息
		{
			fromUserId := initParam.CsTarget + params.UserId
			toGroupId := groupId
			objectName := initParam.ChatMsgTxt
			links := make([]map[string]string, 0)
			csUser, err := customerServiceService.CsUser(router, InstanceDB, gconv.Int(params.UserId))
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
			members := dto.MsgMembers{
				UserName: csUser.RealName,
				//UserAvatar: user.Avatar,
				UserAvatar: initParam.FAEAvatar,
			}
			groupMemberC, err := groupService.GroupMemberC(router, BusinessDB, params.GroupId)
			if err != nil {
				dealRequestError(router, err)
				return
			}
			//msgContent := user.Nickname + initParam.TransMessageText
			//msgContent := initParam.TransMessageText
			msgContent := initParam.Customer + groupMemberC.MemberName + initParam.TransMessageText
			content := dto.Msg{
				Content:    msgContent,
				Links:      links,
				Members:    members,
				Style:      "question",
				Extra:      params.GroupId,
				ObjectName: objectName,
				//ChatType:   "private",
				ChatType:        "group",
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 10:
		// 10 【机器人】发送【正在邀请】状态 【提示】 告知用户
		//发送一句话告诉用户正在邀请人
		//拉人入群之后的发送提示
		//机器人id
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			msgContent := initParam.InvitationWaitMsg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 11:
		// 11 【客户】确认是否转接给予到客服的 【提示】
		//上一步已经创建群聊，不需要在判断一遍
		//A 点击 B 的转接，A ==》 params.UserId，B ==》 params.OtherUserId
		// xx拒绝了您的转接，xx接受了您的转接
		// 您拒绝了xx的转接，您接受了xx的转接
		//操作人的 用户id
		csUser, err := customerServiceService.CsUser(router, InstanceDB, gconv.Int(params.OtherUserId))
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		OtherUserReceive := ""
		if params.Msg == "question_accept_do" {
			groupMemberCs, err := groupService.GroupMemberCs(router, BusinessDB, params.GroupId)
			if err != nil {
				dealRequestError(router, err)
				return
			}
			exitUserId := make([]string, 0)
			for _, v := range groupMemberCs {
				if params.OtherUserId != gconv.String(v.UserID) {
					exitUserId = append(exitUserId, initParam.CsTarget+gconv.String(v.UserID))
				}
			}
			//userAnswer = "您接受了" + otherUserMsg.RealName + "的转接"
			//OtherUserReceive = userMsg.RealName + "接受了您与" + customerName + "的转接"
			//OtherUserReceive = userMsg.RealName + "加入会话"
			OtherUserReceive = csUser.RealName + initParam.ServiceText
			fromUserId := initParam.CTarget + params.UserId
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{
				UserName:   csUser.RealName,
				UserAvatar: csUser.Avatar,
			}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				Members:         members,
				Style:           params.Msg,
				Extra:           params.GroupId,
				ObjectName:      objectName,
				ChatType:        "group",
				QuitGroupUserId: exitUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
			groupCs, err := groupService.GroupCs(router, InstanceDB, params.OtherUserId, params.Extra)
			if err != nil {
				dealRequestError(router, err)
				return
			}
			{
				fromUserId := initParam.CTarget + params.UserId
				toGroupId := groupCs.GroupID
				objectName := initParam.ChatMsgInfo
				msgContent := initParam.TransIsAcceptText
				members := dto.MsgMembers{}
				content := dto.Msg{
					Content:         msgContent,
					Links:           links,
					ObjectName:      objectName,
					ChatType:        "private",
					Members:         members,
					QuitGroupUserId: quitGroupUserId,
					NoticeUserId:    noticeUserId,
					InvisibleUserId: invisibleUserId,
				}
				contentStr, err := json.Marshal(content)
				if err != nil {
					middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
					return
				}
				rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
				err = sendRonCloud(rcParam)
				if err != nil {
					middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
					return
				}
			}
		}
		if params.Msg == "question_refuse_do" {
			//userAnswer = "您拒绝了" + otherUserMsg.RealName + "的转接"
			OtherUserReceive = csUser.RealName + initParam.TransRefuseText
			fromUserId := initParam.CTarget + params.UserId
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{
				UserName:   csUser.RealName,
				UserAvatar: csUser.Avatar,
			}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				Members:         members,
				Style:           params.Msg,
				Extra:           params.GroupId,
				ObjectName:      objectName,
				ChatType:        "group",
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
			groupCs, err := groupService.GroupCs(router, InstanceDB, params.OtherUserId, params.Extra)
			if err != nil {
				dealRequestError(router, err)
				return
			}
			{
				fromUserId := initParam.CTarget + params.UserId
				toGroupId := groupCs.GroupID
				objectName := initParam.ChatMsgInfo
				msgContent := initParam.TransIsRefuseText
				members := dto.MsgMembers{}
				content := dto.Msg{
					Content:         msgContent,
					Links:           links,
					ObjectName:      objectName,
					ChatType:        "private",
					Members:         members,
					QuitGroupUserId: quitGroupUserId,
					NoticeUserId:    noticeUserId,
					InvisibleUserId: invisibleUserId,
				}
				contentStr, err := json.Marshal(content)
				if err != nil {
					middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
					return
				}
				rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
				err = sendRonCloud(rcParam)
				if err != nil {
					middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
					return
				}
			}
		}
		break
	case 12:
		// 12 【机器人】发送关键词触发 【语】
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := params.Msg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 13:
		// 13 【客服】发送一条合并消息 【框】
		//给客服发送一个合并之后的信息
		//有聊天则直接发送到群
		//没有聊天则先创建聊天，再发送消息
		//发送转接消息
		fromUserId := initParam.CsTarget + params.UserId
		toGroupId := params.GroupId
		objectName := initParam.ChatMsgTxt
		msgContent := params.Msg
		links := make([]map[string]string, 0)
		user, err := userService.UserByUserId(router, InstanceDB, params.UserId)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		members := dto.MsgMembers{
			UserName: user.Nickname,
			//UserAvatar: user.Avatar,
			UserAvatar: initParam.FAEAvatar,
		}
		group, err := groupService.GroupByName(router, InstanceDB, params.Extra, params.UserId)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		msgContent = user.Nickname + "和" + group.Name + "的聊天记录"
		content := dto.Msg{
			Content:    msgContent,
			Links:      links,
			Members:    members,
			Style:      "merge",
			Extra:      params.Extra,
			ObjectName: objectName,
			//ChatType:   "private",
			ChatType:        "group",
			QuitGroupUserId: quitGroupUserId,
			NoticeUserId:    noticeUserId,
			InvisibleUserId: invisibleUserId,
		}
		contentStr, err := json.Marshal(content)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
		err = sendRonCloud(rcParam)
		if err != nil {
			middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
			return
		}
		break
	case 14:
		// 14 【机器人】发送【正在转接】状态 【提示】 告知用户
		//发送一句话告诉用户正在邀请人
		//拉人入群之后的发送提示
		//机器人id
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			msgContent := initParam.TransferWaitMsg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 15:
		// 15 【客户】确认是否受邀给予到客服的 【提示】
		//上一步已经创建群聊，不需要在判断一遍
		//A 点击 B 的转接，A ==》 params.UserId，B ==》 params.OtherUserId
		// xx拒绝了您的转接，xx接受了您的转接
		// 您拒绝了xx的转接，您接受了xx的转接
		//操作人的 用户id
		csUser, err := customerServiceService.CsUser(router, InstanceDB, gconv.Int(params.OtherUserId))
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		OtherUserReceive := ""
		if params.Msg == "invitation_accept_do" {
			//userAnswer = "您接受了" + otherUserMsg.RealName + "的邀请"
			OtherUserReceive = csUser.RealName + initParam.AddText
			//fromUserId := CTarget + params.UserId
			fromUserId := initParam.RobotTarget + gconv.String(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{
				UserName:   csUser.RealName,
				UserAvatar: csUser.Avatar,
			}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				Members:         members,
				Style:           params.Msg,
				Extra:           params.GroupId,
				ObjectName:      objectName,
				ChatType:        "group",
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		if params.Msg == "invitation_refuse_do" {
			//userAnswer = "您拒绝了" + otherUserMsg.RealName + "的邀请"
			OtherUserReceive = csUser.RealName + initParam.InvitationRefuseText
			//fromUserId := CTarget + params.UserId
			fromUserId := initParam.RobotTarget + gconv.String(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{
				UserName:   csUser.RealName,
				UserAvatar: csUser.Avatar,
			}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				Members:         members,
				Style:           params.Msg,
				Extra:           params.GroupId,
				ObjectName:      objectName,
				ChatType:        "group",
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 16:
		// 16 【机器人】时间截断 【提示】
		links := make([]map[string]string, 0)
		//不走融云，自己存消息
		msg := dto.Msg{
			ObjectName: initParam.ChatMsgInfo,
			Content:    initParam.TimeBlockMsg,
			Members: dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			},
			ChatType:        "group",
			Links:           links,
			QuitGroupUserId: quitGroupUserId,
			NoticeUserId:    noticeUserId,
			InvisibleUserId: invisibleUserId,
		}
		msgByte, err := json.Marshal(msg)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		err = synchronizeMessages(dto.ChatMessage{
			PreMsg: dto.PreMsg{
				IsCustomerClient: 0,
			},
			FromUserID:   initParam.RobotTarget + strconv.Itoa(groupRobot.ID),
			ToUserID:     params.GroupId,
			ObjectName:   initParam.ChatMsgInfo,
			Source:       "Server",
			MsgTimestamp: gconv.String(time.Now().UnixNano() / 1000),
			MsgUId:       public.RandomString(16),
			Content:      string(msgByte),
		})
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
	case 17:
		// 17 【机器人】客服在没有聊天的情况下，系统与客服发送的一个欢迎内容
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := initParam.CsWelcomeMsg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
	case 18:
		// 18 【客户】超时未接受邀请，超时未接受转接
		csUser, err := customerServiceService.CsUser(router, InstanceDB, gconv.Int(params.UserId))
		if err != nil {
			if !errors.Is(err, gorm.ErrRecordNotFound) {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
		}
		groupMemberCache := getGroupMember(router, params.GroupId)
		if len(groupMemberCache) == 0 {
			middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, errors.New(initParam.ErrorGroupMemberCache))
			return
		}
		invisibleUserId := make([]string, 0)
		for _, v := range groupMemberCache {
			if v.MemberType == 0 {
				invisibleUserId = append(invisibleUserId, initParam.CTarget+gconv.String(v.UserID))
			}
		}
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			msgContent := csUser.Nickname + initParam.TimeoutText
			members := dto.MsgMembers{}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				Style:           "invisible",
				InvisibleUserId: invisibleUserId,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		//该转接已超时
		if params.Msg == "question" {
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.Extra
			objectName := initParam.ChatMsgInfo
			msgContent := initParam.TransIsTimeoutText
			members := dto.MsgMembers{}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "private",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		//修改聊天内容
		err = messageService.UpdateMessageStatus(router, BusinessDB, params.MsgUid, params.GroupId, 4)
		if err != nil {
			middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
			return
		}
		break
	case 19:
		//params.UserId			用户id
		//params.OtherUserId	客服id
		//params.GroupId		群id
		userMsg, err := customerServiceService.CsUserAll(router, InstanceDB)
		if err != nil {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		if len(userMsg) == 0 {
			middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
			return
		}
		OtherUserReceive := ""
		exitUserId := make([]string, 0)
		for _, v := range userMsg {
			OtherUserReceive = OtherUserReceive + v.RealName + ","
			exitUserId = append(exitUserId, initParam.CsTarget+gconv.String(v.UserId))
		}
		OtherUserReceive = OtherUserReceive[0:len(OtherUserReceive)-1] + initParam.QuiteText
		groupMemberC, err := groupService.GroupMemberC(router, InstanceDB, params.GroupId)
		if err != nil {
			dealRequestError(router, err)
			return
		}
		{
			fromUserId := initParam.CTarget + gconv.String(groupMemberC.UserID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				Style:           "quitGroup",
				QuitGroupUserId: exitUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		//refreshList(router, exitUserId, params.GroupId)
		break
	case 20:
		//params.OtherUserId	客服id
		//params.GroupId		群id
		OtherUserReceive := ""
		{
			OtherUserReceive = params.Msg + initParam.QuiteText
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			links := make([]map[string]string, 0)
			members := dto.MsgMembers{}
			msgContent := OtherUserReceive
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				Style:           "quitGroup",
				QuitGroupUserId: []string{initParam.CsTarget + params.UserId},
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 21:
		// 8 【机器人】系统消息 【提示】
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgTxt
			msgContent := params.Msg
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 22:
		// 8 【机器人】系统消息 【提示】
		groupMemberC, err := groupService.GroupMemberC(router, BusinessDB, params.GroupId)
		if err != nil {
			dealRequestError(router, err)
			return
		}
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			objectName := initParam.ChatMsgInfo
			msgContent := ""
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:    msgContent,
				Links:      links,
				ObjectName: objectName,
				ChatType:   "group",
				Members:    members,
				Style:      "refresh",
				NoticeUserId: []string{
					initParam.CTarget + gconv.String(groupMemberC.UserID),
				},
				QuitGroupUserId: quitGroupUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
		break
	case 100:
		// 16 【机器人】时间截断 【提示】
		links := make([]map[string]string, 0)
		{
			fromUserId := initParam.RobotTarget + strconv.Itoa(groupRobot.ID)
			toGroupId := params.GroupId
			toUserId := "c_49246"
			objectName := initParam.ChatMsgTxt
			msgContent := "test"
			members := dto.MsgMembers{
				UserName:   groupRobot.RobotNickname,
				UserAvatar: groupRobot.RobotImg,
			}
			content := dto.Msg{
				Content:         msgContent,
				Links:           links,
				ObjectName:      objectName,
				ChatType:        "group",
				Members:         members,
				QuitGroupUserId: quitGroupUserId,
				NoticeUserId:    noticeUserId,
				InvisibleUserId: invisibleUserId,
			}
			contentStr, err := json.Marshal(content)
			if err != nil {
				middleware.ResponseError(router, middleware.INTERNAL_ERROR_CODE, err)
				return
			}
			rcParam = fmt.Sprintf("fromUserId=%s&toGroupId=%s&toUserId=%s&objectName=%s&content=%s&isIncludeSender=1", fromUserId, toGroupId, toUserId, objectName, string(contentStr))
			err = sendRonCloud(rcParam)
			if err != nil {
				middleware.ResponseError(router, middleware.PARMAS_ERROR_CODE, err)
				return
			}
		}
	default:
		break
	}
	middleware.ResponseSuccess(router, gin.H{"msg": initParam.DealSuccessText})
}
